﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ParamSetCollection.General.Parameters.Normal;
using ParamSetCollection.General.Parameters.Alarms;
using ParamSetCollection.General.ParamTable;
using ParamSetCollection.General.Logs;
using MainController.Hardware.PLC.MEGMEET.Communication;

namespace MainController.Hardware.PLC.MEGMEET.Datas
{


    //转化初级指令队列，用于通信管理类
    //参数与数据缓存关联，实现自动刷新
    //
    class DataHelper
    {
        #region============================== 变量 ====================================

        private string configPath = System.Environment.CurrentDirectory + "\\Config\\Param\\";

        public ushort[] RDataZone = null;

        CommHelper m_commHelper = null;

        //配置数据块--写
        protected ParamGroupUnit CtxGroup = null;


        //SV数据块/配方数据块--读写
        protected ParamGroupUnit SVGroup = null;


        //PV数据块/状态数据块--读
        protected ParamGroupUnit PVGroup = null;


        //protected DataGroup CTRLGroup = null;


        //输入待定
        protected ParamGroupUnit inputGroup = null;

        //输出待定
        protected ParamGroupUnit outputGroup = null;

        protected AlarmsHelper alarmsHelper = null;

        Dictionary<string,ParamGroupUnit> DataGroupList = new Dictionary<string, ParamGroupUnit>();


        string m_moduleName = "JTR";

        bool m_updateEn = false;//可更新参数标志
        #endregion=====================================================================


        #region============================== 属性 ====================================
        #endregion=====================================================================


        #region============================= 内部函数 ================================
        //初始化参数单元
        private void InitDataUnits(ParamHelper table)
        {

            //系统配置
            if (table.CtxParam != null)
            {
                CtxGroup = table.CtxParam;
                DataGroupList.Add("CTX",CtxGroup);
            }

            //可设参数
            if (table.SvParam != null)
            {
                SVGroup = table.SvParam;
                DataGroupList.Add("SV",SVGroup);
            }

       

            //状态类参数
            if (table.PvParam != null)
            {
                PVGroup = table.PvParam;
                DataGroupList.Add("PV",PVGroup);
            }

            //IO输入
            if (table.InParam != null)
            {
                inputGroup = table.InParam;
                DataGroupList.Add("IN",inputGroup);
            }

            //IO输出
            if (table.OutParam != null)
            {
                outputGroup = table.OutParam;
                DataGroupList.Add("OUT",outputGroup);
            }

            alarmsHelper = table.alarmsHelper;

        }

        private void BindingRenewDataSource(List<CommDataUnit> source)
        {
            //当更新数据源到来时
            foreach (var key in DataGroupList.Keys)
            {
                DataGroupList[key].BindingCommData(source);
                DataGroupList[key].OnBitValueSetCmd += DataHelper_OnBitValueSetCmd;
                DataGroupList[key].OnValueSetCmd += DataHelper_OnValueSetCmd;
            }

            //报警类型需要根据不同值的不同区域进行绑定
            alarmsHelper.BindingCommData(source);


            //针对输入输出不是映射到em区的，需要绑定其他的source


        }






        #endregion====================================================================


        #region============================= 保护函数 ================================
        #endregion====================================================================


        #region============================= 公开函数 ================================
        public DataHelper(ParamHelper table, Communication.CommHelper comHelepr)
        {
            m_moduleName = table.MachineType;
            m_commHelper = comHelepr;

            InitDataUnits(table);
            BindingRenewDataSource(comHelepr.RegDataBuff);


        }

        public DataHelper(ParamHelper table, string code,  ushort[] Em)
        {

            this.RDataZone = Em;

            m_moduleName = code;


            InitDataUnits(table);


        }

        public void RenewData()
        {
            //for (int i = 0; i < DataGroupList.Count; i++)
            //{
            //    DataGroupList[i].InputNewDatas(RDataZone);
            //}
        }

        public void InitUIParam(ParamUnit pu)
        {
           
        }



        public void ReduceParamRange(ParamUnit pu)
        {
            //string[] str = pu.Code.Split('.');

            //for (int i = 0; i < DataGroupList.Count; i++)
            //{
            //    if (DataGroupList[i].Code == str[1])
            //    {
            //        DataGroupList[i].ReduceParamRange(pu);


            //    }
            //}
        }
        #endregion====================================================================


        #region============================= 消息响应 ================================

        private void DataHelper_OnValueSetCmd(int addr, byte[] data, string idcType)//byte数组可能需要重新排列
        {
            CommandUnit cmd = new CommandUnit((ushort)addr,data.ToList(),idcType);

            m_commHelper.PushCmd(cmd);

        }

        private void DataHelper_OnBitValueSetCmd(int mainAddr, int bitIndex, byte[] baseValue, bool setValue, string idcType)//byte数组可能需要重新排列
        {
            CommandUnit cmd = new CommandUnit((ushort)mainAddr, (byte)bitIndex, baseValue, setValue,idcType);
            m_commHelper.PushCmd(cmd);
        }
        #endregion====================================================================
    }
}
